package com.ruoyi.framework.web.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.pay.domain.PayCoinCash;
import com.ruoyi.pay.domain.vo.PayCoinCashVO;
import com.ruoyi.pay.domain.vo.PayCoinUserVO;
import com.ruoyi.pay.service.IPayCoinCashService;
import com.ruoyi.pay.service.IPayCoinUserService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wq.domain.WqNoticeChat;
import com.ruoyi.wq.service.IWqNoticeChatService;
import com.ruoyi.wq.service.IWqPlayOrderService;
import com.ruoyi.wq.service.IWqUserFriendService;

@Component
public class CoinCashService {
	
	@Autowired
	private IPayCoinCashService payCoinCashService;
	
	@Autowired
	private ISysUserService sysUserService;
	
	@Autowired
    private IPayCoinUserService payCoinUserService;
	
	@Autowired
	private IWqNoticeChatService wqNoticeChatService;

	public void addCash(PayCoinCashVO payCoinCash) {
		SysUser user = sysUserService.getById(SecurityUtils.getUserId());
		Integer amount = getCash();
		if(payCoinCash.getCoinNum() > amount){
			throw new ServiceException("可提余额不足");
		}
		String tradeNo = IdUtils.tradeNo();
		payCoinCash.setTradeNo(tradeNo);
		payCoinCash.setUserId(user.getUserId());
		payCoinCash.setAppId(user.getAppId());
		payCoinCash.setCashRate(user.getCashRate());
		payCoinCash.setRemark("提现微信："+user.getWechat());
		payCoinCashService.save(payCoinCash);
		
		WqNoticeChat noticeChat = new WqNoticeChat();
		noticeChat.setFromUserId(11637079L);
		noticeChat.setToUserId(user.getUserId());
		noticeChat.setNoticeType("SYSTEM");
		noticeChat.setRemark("提现申请成功！");
		wqNoticeChatService.saveOrUpdate(noticeChat);
	}

	public Integer getCash() {
		PayCoinUserVO payCoinUser = new PayCoinUserVO();
		payCoinUser.setUserId(SecurityUtils.getUserId());
		Integer money = payCoinUserService.getMyCashMoney(payCoinUser);
		Integer cash = payCoinCashService.getMyCashMoney(payCoinUser);
		Integer amount = money - cash;
		if(amount < 0){
			amount = 0;
		}
		return amount;
	}

	public void passCash(PayCoinCashVO payCoinCash) {
		SysUser user = sysUserService.getById(SecurityUtils.getUserId());
		if("0".equals(user.getIsManager())){
			throw new ServiceException("不是管理员");
		}
		PayCoinCash cash = payCoinCashService.getById(payCoinCash.getId());
		cash.setStatus("1");
		payCoinCashService.updateById(cash);
	}

}
